<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateBonusAPIRequest;
use App\Http\Requests\API\UpdateBonusAPIRequest;
use App\Models\Bonus;
use App\Repositories\BonusRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class BonusController
 * @package App\Http\Controllers\API
 */
class BonusAPIAdminController extends ApiBaseController
{
    /** @var  BonusRepository */
    private $bonusRepository;
    protected $model_name = 'App\Models\Bonus';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'name','shop_id',''
    ];

    public function __construct(BonusRepository $bonusRepo)
    {
        $this->bonusRepository = $bonusRepo;
    }

    public function getLimitBonus()
    {
        $name = \request('bond_name');
        $bond = Bonus::where('name',$name)->first();
        if(!$name || !$bond){
            return responseZK(0,null,'请传正确的优惠券名称');
        }
        $id = Auth::id();
        $ok = $this->bonusRepository->dispatchByTypeLimit($name,$id,2,1);
        if($ok)
            return responseZK(1,null,'成功');
        else
            return responseZK(0,null,'超过领取数量');
    }

    public function queryLimitBonus()
    {
        $name = \request('bond_name');
        $bond = Bonus::where('name',$name)->first();
        if(!$name || !$bond){
            return responseZK(0,null,'请传正确的优惠券名称');
        }
        $id = Auth::id();
        $count = Bonus::where([
            ['name',$name],
//            ['type',2],
            ['user_id',$id],
        ])->count();
        return responseZK(1,['count'=>$count],'成功');
    }
}
